package com.zhenwei.zscm.repo;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhenwei.zscm.common.base.MybatisWrapper;
import com.zhenwei.zscm.common.utils.Linq;
import com.zhenwei.zscm.entity.OrderProductCheckEntity;
import com.zhenwei.zscm.mapper.IOrderProductCheckMapper;
import com.zhenwei.zscm.mappings.OrderProductCheckMapping;
import com.zhenwei.zscm.model.dto.order.OrderProductCheckDTO;
import com.zhenwei.zscm.model.vo.orderProductCheck.OneKeyPriceVO;
import com.zhenwei.zscm.model.vo.orderProductCheck.OrderProductCheckVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
* @author DuckyYang
* @description 针对表【order_product_check】的数据库操作Service实现
* @createDate 2025-05-22 09:40:55
*/
@Service
public class OrderProductCheckRepo extends ServiceImpl<IOrderProductCheckMapper, OrderProductCheckEntity> {

    public boolean create(OrderProductCheckVO vo){
        var e = OrderProductCheckMapping.INSTANCE.vo2Entity(vo);
        e.setGuid(IdUtil.simpleUUID());
        return super.save(e);
    }

    public boolean update(OrderProductCheckVO vo) {
        var u = new LambdaUpdateWrapper<OrderProductCheckEntity>()
                .eq(OrderProductCheckEntity::getOrderId, vo.getOrderId())
                .eq(OrderProductCheckEntity::getProductSn, vo.getProductSn())
                .eq(OrderProductCheckEntity::getDeleted, false)
                .set(OrderProductCheckEntity::getQuestion, vo.getQuestion())
                .set(OrderProductCheckEntity::getQuestionCheck, vo.getQuestionCheck())
                .set(OrderProductCheckEntity::getWarrantyCode, vo.getWarrantyCode())
                .set(OrderProductCheckEntity::getWarranty, vo.getWarranty())
                .set(OrderProductCheckEntity::getRepairSiteCode, vo.getRepairSiteCode())
                .set(OrderProductCheckEntity::getRepairSite, vo.getRepairSite())
                .set(OrderProductCheckEntity::getQuoteState, vo.getQuoteState())
                .set(OrderProductCheckEntity::getQuoteDate, vo.getQuoteDate())
                .set(OrderProductCheckEntity::getWorkHourPrice, vo.getWorkHourPrice())
                .set(OrderProductCheckEntity::getIsQuoteConfirm, vo.getIsQuoteConfirm())
                .set(OrderProductCheckEntity::getQuoteConfirmDate, vo.getQuoteConfirmDate());
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    /**
     * 获取工单产品的检测数据
     * @param orderId 工单id
     * @param productSn 产品sn
     * @return OrderProductCheckDTO
     */
    public OrderProductCheckDTO getDetail(String orderId, String productSn) {
        var q = new LambdaQueryWrapper<OrderProductCheckEntity>()
                .eq(OrderProductCheckEntity::getOrderId, orderId)
                .eq(OrderProductCheckEntity::getProductSn, productSn)
                .eq(OrderProductCheckEntity::getDeleted, false);
        var list = this.list(q);
        return Linq.getFirst(list, OrderProductCheckMapping.INSTANCE::entity2Dto);
    }
    /**
     * 获取工单产品的检测数据
     * @param orderId 工单id
     * @return List<OrderProductCheckDTO>
     */
    public List<OrderProductCheckDTO> getList(String orderId) {
        var q = new LambdaQueryWrapper<OrderProductCheckEntity>()
                .eq(OrderProductCheckEntity::getOrderId, orderId)
                .eq(OrderProductCheckEntity::getDeleted, false);
        var list = this.list(q);
        return Linq.mapList(list, OrderProductCheckMapping.INSTANCE::entity2Dto);
    }
    /**
     * 删除工单所有产品数据
     * @param orderId 工单id
     * @return bool
     */
    public boolean delete(String orderId) {
        var u = new LambdaUpdateWrapper<OrderProductCheckEntity>()
                .eq(OrderProductCheckEntity::getOrderId, orderId)
                .eq(OrderProductCheckEntity::getDeleted, false)
                .set(OrderProductCheckEntity::getDeleted, true);
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }

    @Transactional(rollbackFor = Exception.class)
    public boolean saveBatch(List<OrderProductCheckDTO> orderProductCheckList) {
        List<OrderProductCheckEntity> orderProductCheckEntities = Linq.mapList(orderProductCheckList, OrderProductCheckMapping.INSTANCE::dto2Entity);
        return this.saveBatch(orderProductCheckEntities);
    }

    public boolean oneKeyPrice(OneKeyPriceVO vo) {
        var u = new LambdaUpdateWrapper<OrderProductCheckEntity>()
                .eq(OrderProductCheckEntity::getOrderId, vo.getOrderId())
                .eq(OrderProductCheckEntity::getDeleted, false)
                .set(OrderProductCheckEntity::getQuoteState, "是")
                .set(OrderProductCheckEntity::getQuoteDate, vo.getQuoteDate());
        MybatisWrapper.fillUser(u);
        return this.update(u);
    }
}




